#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main() {
    // 定义参数数组
    char *const argv[] = {"ps", "-ef", NULL};
    
    // 定义环境变量数组
    char *const envp[] = {"PATH=/bin:/usr/bin", "TERM=console", NULL};
    
    printf("Testing different exec functions:\n");
    
    // 1. execl - 列表参数，需要完整路径
    printf("\n1. Testing execl (list, full path):\n");
    execl("/bin/ps", "ps", "-ef", NULL);
    perror("execl failed");
    
    // 2. execlp - 列表参数，PATH搜索
    printf("\n2. Testing execlp (list, PATH search):\n");
    execlp("ps", "ps", "-ef", NULL);
    perror("execlp failed");
    
    // 3. execle - 列表参数，自定义环境变量
    printf("\n3. Testing execle (list, custom env):\n");
    execle("/bin/ps", "ps", "-ef", NULL, envp);
    perror("execle failed");
    
    // 4. execv - 数组参数，需要完整路径
    printf("\n4. Testing execv (array, full path):\n");
    execv("/bin/ps", argv);
    perror("execv failed");
    
    // 5. execvp - 数组参数，PATH搜索（最常用）
    printf("\n5. Testing execvp (array, PATH search):\n");
    execvp("ps", argv);
    perror("execvp failed");
    
    // 6. execve - 数组参数，自定义环境变量
    printf("\n6. Testing execve (array, custom env):\n");
    execve("/bin/ps", argv, envp);
    perror("execve failed");
    
    // 如果所有exec调用都失败，执行到这里
    fprintf(stderr, "All exec calls failed!\n");
    exit(EXIT_FAILURE);
    
    
    return 0;
}
